package com.iweb.servlet;

import com.iweb.utils.UrlUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value = "/user/*", loadOnStartup = 1)
public class UserServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("UserServlet is init");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet is do...");
        String action = UrlUtil.url(req.getRequestURI(), "/user");
        if ("/hello".equals(action)) handlerHello(req, resp);
    }

    private void handlerHello(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //TO DO
        req.getRequestDispatcher("/main.jsp").forward(req, resp);
    }

    @Override
    public void destroy() {
        System.out.println("UserServlet is destroy");
    }
}
